
%MACRO missing_stage2 (in,out);

*****************************************************************************
* Program: MISSING_STAGE2					   						
* 																			
* Description: Performs multiple imputation to estimate missing covariate
*				variables prior to Cox-regression.
*						
* Last modified: August 11, 2015												
* Created by: Martin Wegman													
*****************************************************************************;

%let dat = data; * Dataset name stem;

%let v = 28; * From compute_events sas file;

data loc.&dat.&v;
	set loc.&in;
run;

****************************************************************************;
****************************************************************************;
*****         Compute Nelson-Alan estimator to use in MI      **************;
****************************************************************************;
****************************************************************************;

%let v2 = %eval(&v + 1);

ods exclude all;
ods output productlimitestimates=naout;
proc lifetest data=loc.&dat.&v nelson;
	where urines_observed;
	strata imp;
	id client_id;
  	time TA_opi*YA_opi(0); * Use 'ignore missing' case;
run;
ods select all;
proc sort data=loc.&dat.&v; by imp TA_opi; run;
proc sort data=naout (where=(cumhaz ne .)); by imp TA_opi; run;

data loc.&dat.&v2;
	merge loc.&dat.&v(in=keepme) naout (keep = imp TA_opi cumhaz);
            * the keep option drops unneeded variables;
	by imp TA_opi;
	if keepme;
	            * deletes any extra rows from the naout dataset;
	retain cumhazlast 0;
	            * remember most recent value of this;
	if cumhaz eq . then cumhaz = cumhazlast;
	            * plug in most recent value of cumhazlast,
	              if cumhaz is missing;
	cumhazlast = cumhaz;
	            * the current value of cumhaz is now the most recent;
	rename cumhaz = cumhaz_A;
run;

/*
* Review result;
proc print data = loc.&dat.&v2;
	by imp;
	where urines_observed;
 	var client_id TA_opi YA_opi cumhaz_A T0 -- T15 F0 -- F15 od_cat dast_cat;
run;

*/
****************************************************************************;
****************************************************************************;
********         Imputation of missing covariate values            *********;
****************************************************************************;
****************************************************************************;

%let model_num = 	dm0
					ih01_sqrt
					ih02_log
					ih05_totaltimes 
					du16_log 
					du02b_heroin_years 
					cs01_craving
					so_rec_d so_amb_d so_ts_d
					ss_so ss_fam ss_fri;

%let model_discrim = dast_cat_m dm2_m;

%let model_logistic = 
					dm3_m
					dm4_m
					dm5_m  
					rbd1
					daily_heroin  
					lifetime_alcohol 
					lifetime_other_opiates 
					lifetime_benzos 
					lifetime_stimulants ;

					*Issues with convergence:
					lifetime_moreuse
					dt01;
%let v = 29;

proc mi nimpute=0 data=loc.&dat&v seed=55;
	var  site cumhaz_A YA_opi RA &model_num &model_logistic &model_discrim;
run; 

%MACRO stage2_imputation(v);

	%let count = 0;

	%do i = 1 %to 3;

		%do j = 1 %to 3;

			%let combin = "c&i","p&j";
			%put &combin;

			%let count = %eval(&count + 1);

			proc mi nimpute=5 data=loc.&dat&v out = loc.&dat&v._&count seed=55 ;
				where urines_observed and arm in(&combin);
				class site RA &model_discrim &model_logistic; 
				fcs nbiter = 100 PLOTS=TRACE(MEAN STD)
				logistic(&model_logistic) discrim(&model_discrim/classeffects=include);
				var  site cumhaz_A YA_opi RA &model_num &model_discrim &model_logistic ; 
			run; 

		%end;

	%end;

%MEND;

****************************************************************************;

%stage2_imputation(&v);

*Note: nonconvergence with daily_heroin, dm5_m, rbd1 when imp = "c2" (i.e. VTC SV90);

****************************************************************************;

%macro combine(v);

	%let v2 = %eval(&v + 1);

	proc datasets lib=loc nolist;       
		delete &dat&v2;   
	run;
	quit;

	data loc.&dat&v2;
	run;

	%do k = 1 %to 9;
		
		data loc.&dat&v2;
			set loc.&dat&v._&k (in = in_k) loc.&dat&v2;
			if in_k then combination = &k;
		run;

	%end;

	data loc.&dat&v2;
		set loc.&dat&v2;
		if combination = . then delete;
	run;

%mend;

****************************************************************************;

%combine(&v);

data loc.&out;
	set loc.&dat.&v2;
run;

****************************************************************************;
****************************************************************************;
*********************   Remove old datasets               ******************;
****************************************************************************;
****************************************************************************;


proc datasets lib=loc nolist;       
	delete &dat.28 &dat.29: &dat.30 ;   
run;
quit;


****************************************************************************;
****************************************************************************;
**************                END                ***************************;
****************************************************************************;
****************************************************************************;

%MEND missing_stage2;
